package com.stylefeng.guns.rest.modular.App.controller;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.rest.Tools.CodeMsg;
import com.stylefeng.guns.rest.Tools.Result;
import com.stylefeng.guns.rest.common.annotion.SSO;
import com.stylefeng.guns.rest.modular.App.model.OrderInf;
import com.stylefeng.guns.rest.modular.App.service.IOrderInfService;
import com.stylefeng.guns.rest.modular.DTO.ComplaintDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
/**
 * 线路类型
 *
 * @author sasazaixian
 * @Date 2018-11-15 14:16
 */
@Controller
@RequestMapping(value = {"/gunsApi/auth/orderInf"})
@Api(value = "任务订单相关api", tags = {"任务订单相关接口"})
public class OrderInfController extends BaseController {
    @Autowired
    private IOrderInfService orderInfService;

    /**
     * 获取任务详情
     * @param orderInf
     * @return
     */
    @ApiOperation(value = "获取任务详情", notes = "获取任务详情", httpMethod = "POST")
    @PostMapping(value = "/selectListTaskOrder", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @SSO
    public Result selectListTaskOrder(@RequestBody @ApiParam(name = "orderInf", value = "线路类型对象")OrderInf orderInf) {
        try {
            return Result.success(orderInfService.selectListOrderInf(orderInf,getToken()));
        }catch(Exception e){
            return Result.error("获取任务详情列表失败!",500100);
        }
    }

    /**
     * 上车
     * @param tiId  任务单ID
     * @param oiId  订单id
     * @param qrcode 乘车码
     * @param addr  上车地址
     * @param pos   上车经纬度
     * @return
     */
    @ApiOperation(value = "乘客上车", notes = "乘客上车", httpMethod = "POST")
    @PostMapping(value = "/upCar", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @SSO
    public Result upCar(
            @ApiParam(name = "tiId", value = "任务ID", required = true) @RequestParam(value = "tiId") String tiId,
            @ApiParam(name = "oiId", value = "订单Id", required = true) @RequestParam(value = "oiId") String oiId,
            @ApiParam(name = "qrcode", value = "乘车码", required = true) String qrcode,
            @ApiParam(name = "addr", value = "地址", required = true) @RequestParam(value = "addr") String addr,
            @ApiParam(name = "pos", value = "经纬度", required = true) @RequestParam(value = "pos") String pos) {
        try {
            return Result.success(orderInfService.updateOrderInfUp(tiId,oiId,qrcode,addr,pos,getToken()));
        }catch(Exception e){
            return Result.error(e.getMessage(),500100);
        }
    }


    /**
     * 下车
     * @param tiId  任务单ID
     * @param oiId  订单id
     * @param addr  下车地址
     * @param pos   下车经纬度
     * @return
     */
    @ApiOperation(value = "乘客下车", notes = "乘客下车", httpMethod = "POST")
    @PostMapping(value = "/downCar", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @SSO
    public Result downCar(
            @ApiParam(name = "tiId", value = "任务ID", required = true) @RequestParam(value = "tiId") String tiId,
            @ApiParam(name = "oiId", value = "订单Id", required = true) @RequestParam(value = "oiId") String oiId,
            @ApiParam(name = "addr", value = "地址", required = true) @RequestParam(value = "addr") String addr,
            @ApiParam(name = "pos", value = "经纬度", required = true) @RequestParam(value = "pos") String pos) {
        try {
            return Result.success(orderInfService.updateOrderInfDown(tiId,oiId,addr,pos,getToken()));
        }catch(Exception e){
            return Result.error(e.getMessage(),500100);
    }
    }

    /**
     * 删除任务信息表数据
     * @return
     */
    @ApiOperation(value = "司机投诉订单接口", notes = "通过拒绝原因和任务信息拒绝接受任务", httpMethod = "POST")
    @PostMapping(value = "/complaint", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @SSO
    public Result complaint(@RequestBody @Valid ComplaintDTO complaintDTO, BindingResult result) {
        if(result.hasErrors()){
            return Result.error(CodeMsg.PARAMETER_ISNULL.getMessage(),500101);
        }
        try {
            OrderInf orderInf=this.orderInfService.selectById(complaintDTO.getOiId());
            if(orderInf!=null){
                BeanUtil.copyProperties(complaintDTO,orderInf,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true).setIgnoreCase(true));
                orderInf.updateById();
                return Result.success("投诉成功!");
            }
            return Result.error("投诉订单不存在!",100010);
        }catch(Exception e){
            return Result.error(e.getMessage(),500100);
        }
    }
	private String getToken(){		
		return getHttpServletRequest().getHeader("Authorization").substring(7);
    }
}
